// pages/login/login.js
var app = getApp(), api = require("../../../api.js");
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {

    // console.log("Time Out...");  
    that.setData({
      // second: "Time Out..."  
      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });

    return;
  }
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }, 1000)
}


Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    bindcolor: false,
    phone: '',
    yzm:'',
    yhmima: '',
    showtime1: true,
    showtime2: false,
    reg_hqyzm: "获取验证码",
    countdown_time: 60,

    /** 
        * 页面配置 
        */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,

  },
  //事件处理函数
  reset: function () {
    wx.navigateTo({
      url: '../reset/reset'
    })
  },
  reg_yanzhengma_huoqu: function () {
    var phone = this.data.phone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (phone.length == 0) {
      wx.showModal({
        title: '手机号不能为空',
      })
      return false;
    } else if (phone.length < 11) {
      wx.showModal({
        title: '手机号长度有误！',
      })
      return false;
    } else if (!myreg.test(phone)) {
      wx.showModal({
        title: '手机号有误！',
      })
      return false;
    }
    this.setData({
      showtime1: false,
      showtime2: true,

    });
    var that = this;
    app.request({
      url: api.default.checkcode,
      method:'post',
      data:{
        phone:phone,
        bid:wx.getStorageSync('bid')
      },
      success:function(e){
        console.log('验证码',e)
        if(e.code == 0){
          wx.showToast({
            title: '网络错误，错误码1005',
            icon: 'loading',
            duration: 2000
          })
          return;
        }
        if(e.code>0){
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 2000
          })
        }
        that.setData({
          yzm: e.code,
        })
      }
    })
    countdown(this);
    // wxTimer.start(this);  
  },
  onLoad: function () {
    
    var that = this;
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    });
    app.pageOnLoad(this);
    /** 
     * 获取系统信息 
     */
  },
  showok: function (e) { 
var that = this
    console.log('shuru',e.detail.value.yz )
    console.log('fa', that.data.yzm)
    if (e.detail.value.yz==''){
      wx.showModal({
        title: '请输入验证码',  
      })
      return false;
    }
    if (e.detail.value.yz!=this.data.yzm){
      wx.showModal({
        title: '验证码错误',
      })
      return false;
    }
    app.request({
      url: api.default.phone,
      method:'post',
      data:{
        'bid':wx.getStorageSync('bid'),
        'phone':this.data.phone,
        'uid':wx.getStorageSync('uid')
      },
      success:function(e){
        console.log(e);
        if(e.code==1){
          wx.showToast({
            title: '绑定成功',
            duration:2000,
            success:function(){
              setTimeout(function(){
                wx.setStorageSync('phone', e.phone);
                app.request({
                  url: api.default.userinfo,
                  data:{
                    uid:wx.getStorageSync('uid')
                  },
                  success:function(e){
                    wx.setStorage({
                      key: 'cash',
                      data: e.data,
                    })
                  }
                })
                wx.redirectTo({
                  url: '/service/pages/service/authentication/index'
                })
              },2000)
            }
          })
        }else{
          wx.showToast({
            title: '已绑定手机号',
            duration: 1000,
          })
        }
      }
    })
    // wx.showToast({
    //   title: '绑定成功',
    //   icon: 'success',
    //   duration: 2000
    // })
  },
  blur1: function (e) {
  
    var that = this
    that.setData({
      phone: e.detail.value
    })
  },
  blur2: function (e) {
    this.setData({
      yhmima: e.detail.value
    })
    var yhname = this.data.yhname;
    var yhmima = this.data.yhmima;
    if (yhname == '' || yhmima == '') {
      this.setData({
        bindcolor: false
      })
    } else {
      this.setData({
        bindcolor: true
      })
    }
  },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {

  }
  ,
  

}); 

